package chapter1.bagqueuestack.exercise;

import chapter1.bagqueuestack.example.Stack;
import edu.princeton.cs.algs4.StdIn;

/**
 * 将算术表达式由中序表达式转换为后序表达式
 *
 * @author quanlinlin on 2018/12/2 17:41.
 * @version 1.0
 * @apiNote % java InfixToPostfix
 * ( 2 + ( ( 3 + 4 ) * ( 5 * 6 ) ) )
 * [Ctrl-d]
 * 2 3 4 + 5 6 * * +
 * <p>
 * % java InfixToPostfix | java EvaluatePostfix
 * ( 2 + ( ( 3 + 4 ) * ( 5 * 6 ) ) )
 * [Ctrl-d]
 * 212
 */
public class InfixToPostfix {

    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        while (!StdIn.isEmpty()) {
            String s = StdIn.readString();
            if (s.equals("+")) {
                stack.push(s);
            } else if (s.equals("*")) {
                stack.push(s);
            } else if (s.equals(")")) {
                System.out.print(stack.pop() + " ");
            } else if (s.equals("(")) {
                System.out.print("");
            } else {
                System.out.print(s + " ");
            }
        }
        System.out.println();
    }

}
